﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;

using System.IO;
using System.Data;
using System.Xml;
using System.Web.Security;
using System.Security.Cryptography;
using System.Net;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Gift.WebApp
{
    /// <summary>
    ///ThumbnailImage 的摘要说明
    /// </summary>
    public class ThumbnailImage
    {
        // Methods
        public bool GetThumbnail(string sourceFile, string destFile, int destHeight, int destWidth, int thumbnailType, Color color)
        {
            Bitmap bitmap;
            bool flag;
            Image image = Image.FromFile(sourceFile);
            ImageFormat rawFormat = image.RawFormat;
            int width = 0;
            int height = 0;
            int num3 = image.Width;
            int num4 = image.Height;
            int srcX = 0;
            int srcY = 0;
            if ((num4 > destHeight) || (num3 > destWidth))
            {
                if ((num3 * destHeight) > (num4 * destWidth))
                {
                    width = destWidth;
                    height = (destWidth * num4) / num3;
                    srcX = (image.Width - ((image.Height * destWidth) / destHeight)) / 2;
                }
                else
                {
                    height = destHeight;
                    width = (num3 * destHeight) / num4;
                    srcY = (image.Height - ((image.Width * destHeight) / destWidth)) / 2;
                }
            }
            else
            {
                width = num3;
                height = num4;
            }
            if (thumbnailType == 2)
            {
                bitmap = new Bitmap(width, height);
            }
            else
            {
                bitmap = new Bitmap(destWidth, destHeight);
            }
            Graphics graphics = Graphics.FromImage(bitmap);
            if (string.Compare(Path.GetExtension(sourceFile), ".PNG", true) != 0)
            {
                graphics.Clear(color);
            }
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            int num7 = (image.Width - destWidth) / 2;
            int num8 = (image.Height - destHeight) / 2;
            switch (thumbnailType)
            {
                case 2:
                    graphics.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                    break;

                case 3:
                    graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                    break;

                case 4:
                    graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), num7, num8, destWidth, destHeight, GraphicsUnit.Pixel);
                    break;

                case 5:
                    graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, 0, destWidth, destHeight, GraphicsUnit.Pixel);
                    break;

                case 6:
                    graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, num8 * 2, destWidth, destHeight, GraphicsUnit.Pixel);
                    break;

                case 7:
                    graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), srcX, srcY, image.Width - (2 * srcX), image.Height - (2 * srcY), GraphicsUnit.Pixel);
                    break;

                default:
                    graphics.DrawImage(image, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                    break;
            }
            graphics.Dispose();
            EncoderParameters encoderParams = new EncoderParameters();
            long[] numArray = new long[] { 100L };
            EncoderParameter parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, numArray);
            encoderParams.Param[0] = parameter;
            try
            {
                if (string.Compare(Path.GetExtension(sourceFile), ".BMP", true) == 0)
                {
                    bitmap.Save(destFile, ImageCodecInfo.GetImageEncoders()[0], encoderParams);
                }
                if (string.Compare(Path.GetExtension(sourceFile), ".JPG", true) == 0)
                {
                    bitmap.Save(destFile, ImageCodecInfo.GetImageEncoders()[1], encoderParams);
                }
                if (string.Compare(Path.GetExtension(sourceFile), ".GIF", true) == 0)
                {
                    bitmap.Save(destFile, ImageCodecInfo.GetImageEncoders()[2], encoderParams);
                }
                if (string.Compare(Path.GetExtension(sourceFile), ".TIF", true) == 0)
                {
                    bitmap.Save(destFile, ImageCodecInfo.GetImageEncoders()[3], encoderParams);
                }
                if (string.Compare(Path.GetExtension(sourceFile), ".PNG", true) == 0)
                {
                    bitmap.Save(destFile, ImageCodecInfo.GetImageEncoders()[4], encoderParams);
                }
                flag = true;
            }
            catch
            {
                flag = false;
            }
            finally
            {
                image.Dispose();
                bitmap.Dispose();
            }
            return flag;
        }

    }
}